#include <gtk/gtk.h>
#include <glib/gprintf.h>
#include "notifier.h"

#ifndef GSM_USE_INTERNAL_NOTIFIER
	#include <libnotify/notify.h>
#endif

#ifdef G_OS_WIN32
	#include <windows.h>
#endif

typedef struct {
	gchar *summary;
	gchar *body;
} Notification;

#ifdef GSM_USE_INTERNAL_NOTIFIER
static guint source_id = 0;
static GList *notifications = NULL;
static GtkWidget *window = NULL;
static GtkWidget *summary;
static GtkWidget *body;
#endif

static gchar *sound_path = NULL;


void
gsm_notifier_set_sound (const gchar *sound)
{
	if (sound_path)
		g_free (sound_path);
	sound_path = g_strdup (sound);
}

gchar *
gsm_notifier_get_sound ()
{
	return sound_path;
}


#ifdef GSM_USE_INTERNAL_NOTIFIER

static gboolean
update (gpointer data)
{
	if (!notifications) {
		gtk_widget_hide (window);
		g_source_remove (source_id);
		source_id = 0;
		return FALSE;
	}
	
	Notification *notification = (Notification *)notifications->data;
	gtk_label_set_text (GTK_LABEL (summary), notification->summary);
	gtk_label_set_text (GTK_LABEL (body), notification->body);
	
	notifications = g_list_remove (notifications, notification);
	g_free (notification->summary);
	g_free (notification->body);
	g_slice_free (Notification, notification);
	
	return TRUE;
}

static void
create_window ()
{
	PangoAttribute *attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
	PangoAttrList *attrs = pango_attr_list_new ();
	pango_attr_list_insert (attrs, attr);
	
	summary = gtk_label_new (NULL);
	gtk_label_set_attributes (GTK_LABEL (summary), attrs);
	gtk_widget_show (summary);
	
	body = gtk_label_new (NULL);
	gtk_misc_set_alignment (GTK_MISC (body), 0.f, 0.f);
	gtk_widget_show (body);
	
	GtkWidget *vbox = gtk_vbox_new (FALSE, 12);
	gtk_box_pack_start (GTK_BOX (vbox), summary, FALSE, TRUE, 0);
	gtk_box_pack_start (GTK_BOX (vbox), body, TRUE, TRUE, 0);
	gtk_widget_show (vbox);
	
	window = gtk_window_new (GTK_WINDOW_POPUP);
	gtk_container_set_border_width (GTK_CONTAINER (window), 4);
	gtk_widget_set_size_request (window, 200, -1);
	gtk_container_add (GTK_CONTAINER (window), vbox);
	
	GdkScreen *screen = gdk_screen_get_default ();
	gtk_widget_set_uposition (window,
			gdk_screen_get_width (screen) - 205,
			gdk_screen_get_height (screen) - 110);
}

#endif


void
gsm_notifier_show_message (const gchar *summary, const gchar *body)
{
#ifdef GSM_USE_INTERNAL_NOTIFIER

	if (!window)
		create_window ();
	
	Notification *notification = g_slice_new0 (Notification);
	notification->summary = g_strdup (summary);
	notification->body = g_strdup (body);
	notifications = g_list_append (notifications, notification);
	
#ifdef G_OS_WIN32
	PlaySound (sound_path, NULL, SND_FILENAME|SND_ASYNC);
#endif
	
	if (source_id == 0) {
		source_id = g_timeout_add_seconds (4, update, NULL);
		gtk_widget_show (window);
		update (NULL);
	}
	
#else

	NotifyNotification *n = notify_notification_new (summary, body, NULL, NULL);
	notify_notification_set_timeout (n, 3000);
	notify_notification_show (n, NULL);
	g_object_unref (n);
	
#endif
}